import router from '@/router'
import store from '@/store'
import nprogress from 'nprogress'
import 'nprogress/nprogress.css'
import { canceled } from '@/utils/request'

const pageArr = ['/404', '/login']

// 路由导航守卫
router.beforeEach(async(to, from, next) => {
  nprogress.start()
  canceled()
  if (store.getters.token) {
    if (to.path.toLowerCase() === '/login') {
      next(from.path.toLowerCase())
    } else {
      // console.log('打印', store.getters.userInfo)
      if (!store.getters.userInfo) {
        await store.dispatch('user/actUserInfo')
      }
      next()
    }
  } else {
    pageArr.includes(to.path) ? next() : next('/login?redirect=' + to.fullPath)
  }
  nprogress.done()
})
router.afterEach((to, from) => {
  nprogress.done()
})
